﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Xms.Abp.Storage.Migrator;

public class StorageMigrator<TStorage> : IStorageMigrator
    where TStorage : XmsStorage<TStorage>
{
    protected IServiceProvider ServiceProvider { get; }
    protected ILogger<StorageMigrator<TStorage>> Logger { get; }
    public StorageMigrator(
        IServiceProvider serviceProvider,
        ILogger<StorageMigrator<TStorage>> logger
        )
    {
        ServiceProvider = serviceProvider;
        Logger = logger;
    }
    public async Task MigrateAsync()
    {
        var storage = ServiceProvider.GetRequiredService<TStorage>();
        await storage.Database.MigrateAsync();
        //var pendingMigrations = await storage.Database.GetPendingMigrationsAsync();
        //if (pendingMigrations.Any())
        //{
        //    Logger.LogInformation("Migrations ({migrations}) will migrate to database.", pendingMigrations.JoinAsString(","));
        //    await storage.Database.MigrateAsync();
        //}
        //else
        //{
        //    Logger.LogInformation("No migration will migrate.");
        //}
    }
}

